package flex.cc.uac.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import flex.cc.base.enums.ErrorCodeEnum;
import flex.cc.uac.dao.UacRoleActionDao;
import flex.cc.uac.entity.UacRoleActionEntity;
import flex.cc.uac.exceptions.UacBizException;
import flex.cc.uac.service.UacRoleActionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service("uacRoleActionService")
@Transactional(rollbackFor = Exception.class)
public class UacRoleActionServiceImpl extends ServiceImpl<UacRoleActionDao, UacRoleActionEntity> implements UacRoleActionService {

    @Override
    public void deleteByRoleId(Long roleId) {
        if (roleId == null) {
            throw new UacBizException(ErrorCodeEnum.UAC10012001);
        }

        Map<String,Object> map = new HashMap<>();
        map.put("role_id",roleId);
        this.removeByMap(map);

    }

    @Override
    public void deleteByRoleIdList(List<Long> roleIdList) {
        Wrapper queryWrapper = new QueryWrapper<UacRoleActionEntity>()
                .in("role_id",roleIdList);
        this.remove(queryWrapper);
    }

    @Override
    public List<UacRoleActionEntity> listByRoleId(Long roleId) {
        Wrapper queryWrapper = new QueryWrapper<UacRoleActionEntity>()
                .eq("role_id",roleId);
        return this.list(queryWrapper);
    }

    @Override
    public void insert(Long roleId, Set<Long> actionIdList) {
        if (roleId == null) {
            throw new UacBizException(ErrorCodeEnum.UAC10012001);
        }
        UacRoleActionEntity uacRoleAction = new UacRoleActionEntity();
        uacRoleAction.setRoleId(roleId);
        for (Long actionId : actionIdList) {
            uacRoleAction.setActionId(actionId);
            this.save(uacRoleAction);
        }
    }

}
